/** 
 * 修改-新增 合同
*/
<template>
    <vp-dialog :span="3" ref="dialogRef">    
        <template #title>
            <vp-label v-if="type == 'add'" zh="新增" en="Add" align="left"/>
            <vp-label v-if="type == 'edit'" zh="修改" en="Edit" align="left"/>
            <vp-label v-if="type == 'revision'" zh="修订" en="Edit" align="left"/>
            
            
        </template>
        <contract ref="commonRef" :id="id" :type="type"></contract>
        <template #footer>
            <vp-btn :loading="saveLoading" v-if="type!='revision'"  @click="handleSave(10)"><vp-label zh="暂存" en="Add"></vp-label></vp-btn>
            <vp-btn-submit :loading="saveLoading" @click="handleSave(20)"/>
        </template>
    </vp-dialog>
</template>

<script setup lang="ts" name="EditContract">
import { defineAsyncComponent, defineExpose, ref } from 'vue'
import request from '/@/utils/request';

const contract = defineAsyncComponent(() => import('./contract.vue'));

const emits = defineEmits<{
    (e: 'formSubmited'): void
}>()

const dialogRef = ref();
const type = ref('add');
const id = ref('');
const saveLoading = ref(false);

const commonRef = ref();

const handleOpen = (_type: string, _id: string)=>{
    id.value = _id;
    type.value = _type;
    dialogRef.value.handleOpen();
}

const handleSave = (zt:number) =>{
    saveLoading.value = true;
    commonRef.value.getData().then((data: any) =>{
        request.post("/material/contract/" + type.value, {
            ...data,
            zt: zt
        }).then(() => {
            emits('formSubmited')
            dialogRef.value.handleClose();
        })
        .catch(() => {})
        .finally(() => {
            saveLoading.value = false;
        });
    }).catch(()=>{saveLoading.value = false;});    
}

defineExpose({
    handleOpen
})
</script>
<style scoped lang="scss">

</style>
